﻿

/***
 * 获得活动列表
 */
function getActivityList(){
	$.ajax({
		type:"post",
		url:base_path+"/action/findActivity.do",
		dataType:"json",
		data:{},
		success:function(result) {
			if(result.status==0) {
				var list = result.data;
				$(list).each(function(i){
					var color;
					if(i%4==0){
						color='bg-primary';
					}else if(i%4==1){
						color='bg-danger';
					}
					else if(i%4==2){
						color='bg-inverse';
					}else{
						color='bg-warning';
					};
					
					var column=i%3;
					$('#col_'+column).append('<div id="contentfeeds'+i+'" class="panel panel-animated panel-default animated fadeInUp" style="visibility: visible;"><div class="panel-body bordered-bottom"><div class="no-padding jumbotron '+color+'"><p class="lead"><a href="activity_detail.html#'+this.cn_activity_id+'">'+this.cn_activity_title+'</a></p></div><p class="text-muted">'+this.cn_activity_body+'</p><div class="text-muted"><small style="color:red;">活动结束时间:'+this.cn_activity_end_time+'</small></div></div></div>');
				});
			} else {
				alert(result.message);
			}
		},
		error:function(xhr,status,error) {
			alert("请求失败.");
		}
	});
}

/***
 * 查询指定活动下已参加活动的笔记列表
 */
function getNoteActivitys(activityId,currentPage){
	$.ajax({
		type:"post",
		url:base_path+"/action/listActionDetail.do",
		dataType:"json",
		data:{"actionId":activityId,"page":currentPage},
		success:function(result) {
			if(result.code==0)
			{
				var noteActivityList = result.data;
				// $("#first_action ul").empty();
				if(currentPage>1 && noteActivityList.length==0)
				{
					alert("已经没有更多的数据啦~!");
					return;
				}
				$(noteActivityList).each(function(){
					$("#first_action ul").append('<li class="online"><a ><i class="fa fa-file-text-o" title="online" rel="tooltip-bottom"></i> '+this.cn_note_activity_title+'<button type="button" class="btn btn-default btn-xs btn_position_3 btn_up"><i class="fa fa-thumbs-o-up"></i><span id="up_num">'+this.cn_note_activity_up+'</span></button><button type="button" class="btn btn-default btn-xs btn_position_2 btn_down"><i class="fa fa-thumbs-o-down"></i><span id="up_num">'+this.cn_note_activity_down+'</span></button><button type="button" class="btn btn-default btn-xs btn_position btn_like"><i class="fa fa-star-o"></i></button></a></li>');
					$("#first_action ul li:last").data("noteActivity",this);
				});
			} else {
				alert(result.msg);
			}
		},
		error:function(xhr,status,error) {
			alert("请求失败.");
		}
	});
}

/***
 * 查询活动笔记内容
 */
function getNoteActivityDetail(noteActivityId){
	$.ajax({
		type:"post",
		url:base_path+"activity/findNoteActivityDetail.do",
		dataType:"json",
		data:{"noteActivityId":noteActivityId},
		success:function(result) {
			if(result.status==0) {
				var noteActivity = result.data;
				$("#content_body").append('<h4><strong>标题: </strong>'+noteActivity.cn_note_activity_title+'</h4>');
				$("#content_body").append(noteActivity.cn_note_activity_body);
			} else {
				alert(result.message);
			}
		},
		error:function(xhr,status,error) {
			alert("请求失败.");
		}
	});
}

/***
 * 查询可选择的笔记本
 */
function getSelectNoteBook(){
	$.ajax({
		type:"post",
		url:base_path+"notebook/findList.do",
		dataType:"json",
		data:{},
		success:function(result) {
			if(result.status==0) {
				var list = result.data;
				$(list).each(function(){
					$("#select_notebook ul").append('<li class="online"><a ><i class="fa fa-book" title="online" rel="tooltip-bottom"></i> '+this.cn_notebook_name+'</a></li>');
					$('#select_notebook li:last').data('notebook',this);
				});
			} else {
				alert(result.message);
			}
		},
		error:function(xhr,status,error) {
			alert("请求失败.");
		}
	});
}

/***
 * 查询可选择的笔记
 */
function getSelectNoteList(noteBookId){
	$.ajax({
		type:"post",
		url:base_path+"note/findNote.do",
		dataType:"json",
		data:{"noteBookId":noteBookId},
		success:function(result) {
			if(result.status==0) {
				var list = result.data;
				$(list).each(function(){
					$("#select_note ul").append('<li class="online"><a ><i class="fa fa-file-text-o" title="online" rel="tooltip-bottom"></i> '+this.cn_note_title+'</a></li>');
					$("#select_note ul li:last").data("note",this);
				});
			} else {
				alert(result.message);
			}
		},
		error:function(xhr,status,error) {
			alert("请求失败.");
		}
	});
}





$("#action_ul").on("click","li",function ()
{
	$("#action_ul a").removeClass("checked");
	$(this).find("a").addClass("checked");
	var actionDetailId = $(this).data("noteActivity").cn_note_activity_id;
	//加载笔记
	$.ajax({
		type:"post",
		url:base_path+"/action/showActionDetail.do",
		dataType:"json",
		data:{"actionDetailId":actionDetailId},
		success:function(result)
		{
			$("#content_body").empty();
			if(result.code==0)
			{
				var data = result.data;
				var str = '<h4><strong>笔记标题: </strong>'+data.cn_note_activity_title+'</h4>';
				$("#content_body").append(str);
				$("#content_body").append('<div>'+data.cn_note_activity_body+'</div>');
			} else {
				alert(result.msg);
			}
		},
		error:function(xhr,status,error) {
			alert("请求失败.");
		}
	});
});

/***
 *	收藏活动笔记
 */
$("#action_ul").on("click",".btn_like",function ()
{
	//判断是否登录
	var userId = getCookie("userId");
	//判断是否获取到有效的userId
	if (userId == null) {
		//转发回登录页面
		window.location.href = "index.html";
	}
	var actionDetailId = $(this).parent().parent().data("noteActivity").cn_note_activity_id;
	$.ajax({
		type:"post",
		url:base_path+"/note/collectForAction.do",
		dataType:"json",
		data:{"userId":userId,"actionDetailId":actionDetailId},
		success:function(result) {
			if(result.code==0) {
				alert(result.data);
			} else {
				alert(result.msg);
			}
		},
		error:function(xhr,status,error) {
			alert("请求失败.");
		}
	});
	$('#modalBasic_14 .cancle').trigger('click');
});
$("#action_ul").on("click",".btn_up",function ()
{
	//判断是否登录
	var userId = getCookie("userId");
	//判断是否获取到有效的userId
	if (userId == null) {
		//转发回登录页面
		window.location.href = "index.html";
	}
	var actionDetailId = $(this).parent().parent().data("noteActivity").cn_note_activity_id;
	var $span = $(this).parent().find("#up_num");
	$.ajax({
		type:"post",
		url:base_path+"/action/upActionDetail.do",
		dataType:"json",
		data:{"userId":userId,"actionDetailId":actionDetailId},
		success:function(result) {
			if(result.code==0) {

				var num =Number($span.html());
				$span.html(num+1);
				alert(result.data)
			} else {
				alert(result.msg);
			}
		},
		error:function(xhr,status,error) {
			alert("请求失败.");
		}
	});
});

/***
 *	踩笔记
 */
$("#action_ul").on("click",".btn_down",function ()
{
	//判断是否登录
	var userId = getCookie("userId");
	//判断是否获取到有效的userId
	if (userId == null) {
		//转发回登录页面
		window.location.href = "index.html";
	}
	var $span = $(this).parent().find("#down_num");
	var actionDetailId = $(this).parent().parent().data("noteActivity").cn_note_activity_id;
	$.ajax({
		type:"post",
		url:base_path+"/action/downActionDetail.do",
		dataType:"json",
		data:{"userId":userId,"actionDetailId":actionDetailId},
		success:function(result) {
			if(result.code==0)
			{
				var num = Number($span.html());
				$span.html(num+1);
				alert(result.data)
			} else {
				alert(result.msg);
			}
		},
		error:function(xhr,status,error) {
			alert("请求失败.");
		}
	});
});